﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using LogisticsBLL;


namespace WebLosgic.TruckTeam
{
    public partial class Chedui : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["names"] != null)
                {
                    string ming = Session["names"].ToString();
                    Names.Text = ming;
                }
                
                ddlTruck();
                 BindTruck();
            }
            
        }

        public void BindTruck()
        {
            int pageIndex = AspNetPager1.CurrentPageIndex;
            int pageSize = AspNetPager1.PageSize;
            TruckBLL tb = new TruckBLL();
            string truckname = DdlTruck.SelectedValue;
            string leader = TextLeader.Text;
            int count = 0;
            DataTable dt =TruckBLL.Truread(pageIndex,pageSize,out count,truckname,leader);
            GvTruckList.DataSource = dt;
            AspNetPager1.RecordCount = count;
            GvTruckList.DataBind();
        }

        public void ddlTruck()
        {
            TruckBLL tb = new TruckBLL();
            DataTable dt = tb.ddlbindCarName();
            DdlTruck.DataSource = dt;
            DdlTruck.DataTextField = "TeamName";
            DdlTruck.DataValueField = "TeamName";
            DdlTruck.DataBind();
            DdlTruck.Items.Insert(0, new ListItem("请选择","0"));
        }

        protected void Btncha_Click(object sender, EventArgs e)
        {
            BindTruck();
        }

        protected void GvTruckList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GvTruckList.EditIndex = -1;
            BindTruck();
        }

        protected void GvTruckList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GvTruckList.EditIndex = e.NewEditIndex;
            BindTruck();
        }

        protected void GvTruckList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int TeamID = (int)GvTruckList.DataKeys[e.RowIndex]["TeamID"];
            TruckBLL trb = new TruckBLL();
            bool st = trb.readtru(TeamID);
            if(st==true)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "eer", "alert('删除失败！因为该车队中有车辆，请先去删除车辆。')", true);
            }
            else
            {
            int reualt = TruckBLL.TruckDel(TeamID);
            if (reualt > 0)
            {
                    string name = Session["names"].ToString();
                    string Behavor = "" + name + ":删除车队";
                    int TypeId = 6;
                    int UserID = Convert.ToInt32(Session["UserIDs"]);
                    string Parameters = TeamID.ToString();
                    string ProcName = "p_delTruck";
                    HttpRequest request = HttpContext.Current.Request;
                    string result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                    if (string.IsNullOrEmpty(result)) { result = request.ServerVariables["REMOTE_ADDR"]; }
                    if (string.IsNullOrEmpty(result)) { result = request.UserHostAddress; }
                    if (string.IsNullOrEmpty(result)) { result = "192.168.0.0"; }
                    string IP = "192.168.0.1";
                    int IsExcePion = 0;
                    int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                    if (a > 0)
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "eer", "alert('删除成功！')", true);
                    }
                }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "eer", "alert('删除失败')", true);
            }
            BindTruck();
            }
            
        }

        protected void GvTruckList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int TeamID = (int)GvTruckList.DataKeys[e.RowIndex]["TeamID"];
            string remark = ((TextBox)GvTruckList.Rows[e.RowIndex].FindControl("TextRemark")).Text;
            int reualt = TruckBLL.TruckUpdate(TeamID,remark);
            if (reualt > 0)
            {
                string name= Session["names"].ToString();
                string Behavor = "" + name + ":修改车队信息";
                int TypeId = 7;
                int UserID =Convert.ToInt32(Session["UserIDs"]);
                string Parameters = remark;
                string ProcName = "p_updateTruck";
                HttpRequest request = HttpContext.Current.Request;
                string result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (string.IsNullOrEmpty(result)) { result = request.ServerVariables["REMOTE_ADDR"]; }
                if (string.IsNullOrEmpty(result)) { result = request.UserHostAddress; }
                if (string.IsNullOrEmpty(result)) { result = "192.168.0.0"; }
                string IP = "192.168.0.1";
                int IsExcePion = 0;
                int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                if (a > 0)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "eer", "alert('修改成功！')", true);
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "eer", "alert('修改失败！')", true);
                }
                GvTruckList.EditIndex = -1;
            }
            BindTruck();
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindTruck();
        }
    }
}